//
//  RBLoginVC.m
//  zhongzhenglu
//
//  Created by XL on 2022/9/16.
//

#import "RBLoginVC.h"
#import "RBDefine.h"
#import "RBNavigationBar.h"
#import "UIView+RBAdd.h"
#import <UINavigationController+FDFullscreenPopGesture.h>
#import <Masonry.h>
#import "RBGradientView.h"
#import <UIColor+YYAdd.h>
#import <UIView+YYAdd.h>
#import "RBWechatLoginVC.h"
#import "RBPasswordLoginVC.h"
#import "RBSmsLoginVC.h"
#import "RBRegisterSetPasswordVC.h"
#import "RBRegisterCompleteInfoVC.h"
#import "RBRegisterStateVC.h"
#import "UINavigationController+RBAdd.h"
#import "RBAccountManager.h"

@interface RBLoginVC ()

@property(nonatomic, strong)RBWechatLoginVC *wechatVC;
@property(nonatomic, strong)RBPasswordLoginVC *passwordVC;
@property(nonatomic, strong)RBSmsLoginVC *smsVC;

@end

@implementation RBLoginVC

#pragma mark - Lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self setup];
}

#pragma mark - Private

- (void)setup
{
    self.fd_prefersNavigationBarHidden = YES;
    [self switchToWechatPage];
}

#pragma mark - Network

#pragma mark - Event

- (void)switchToWechatPage
{
    if (!self.wechatVC.view.superview)
    {
        [self.view addSubview:self.wechatVC.view];
        [self.wechatVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    _wechatVC.view.hidden = NO;
    _passwordVC.view.hidden = YES;
    _smsVC.view.hidden = YES;
}

- (void)switchToPasswordPage
{
    if (!self.passwordVC.view.superview)
    {
        [self.view addSubview:self.passwordVC.view];
        [self.passwordVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    _wechatVC.view.hidden = YES;
    _passwordVC.view.hidden = NO;
    _smsVC.view.hidden = YES;
}

- (void)switchToSmsPage
{
    if (!self.smsVC.view.superview)
    {
        [self.view addSubview:self.smsVC.view];
        [self.smsVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
    }
    _wechatVC.view.hidden = YES;
    _passwordVC.view.hidden = YES;
    _smsVC.view.hidden = NO;
}

- (void)loginEvent: (RBLoginResponse *)data removeVcCount: (NSInteger)removeVcCount
{
    __weak typeof(self) weak_self = self;
    if(data.data.is_setpwd)
    {
        RBRegisterSetPasswordVC *vc = RBRegisterSetPasswordVC.new;
        vc.loginResponse = data;
        vc.passwordSetBlock = ^(RBLoginResponse * _Nonnull loginResponse) {
            [weak_self loginEvent:loginResponse removeVcCount:1];
        };
        [self.navigationController pushToViewController:vc animated:YES removeVcCount:removeVcCount];
    }
    else
    {
        if(data.data.step == 1)
        {
            RBRegisterCompleteInfoVC *vc = RBRegisterCompleteInfoVC.new;
            vc.loginResponse = data;
            [self.navigationController pushToViewController:vc animated:YES removeVcCount:removeVcCount];
        }
        else if(data.data.step == 3)
        {//通过
            [RBAccountManager setUserId:data.data.user_id];
            [RBAccountManager setToken:data.data.token];
            [self dismissViewControllerAnimated:YES completion:nil];
        }
        else
        {
            RBRegisterStateVC *vc = RBRegisterStateVC.new;
            vc.loginResponse = data;
            [self.navigationController pushToViewController:vc animated:YES removeVcCount:removeVcCount];
        }
    }
}

#pragma mark - Setter

#pragma mark - Getter

- (RBWechatLoginVC *)wechatVC
{
    if (!_wechatVC)
    {
        self.wechatVC = ({
            __weak typeof(self) weakSelf = self;
            RBWechatLoginVC *vc = RBWechatLoginVC.new;
            vc.passwordLoginBlock = ^{
                [weakSelf switchToPasswordPage];
            };
            vc.smsLoginBlock = ^{
                [weakSelf switchToSmsPage];
            };
            [self addChildViewController:vc];
            vc;
        });
    }
    return _wechatVC;
}

- (RBPasswordLoginVC *)passwordVC
{
    if (!_passwordVC)
    {
        self.passwordVC = ({
            __weak typeof(self) weakSelf = self;
            RBPasswordLoginVC *vc = RBPasswordLoginVC.new;
            vc.wechatLoginBlock = ^{
                [weakSelf switchToWechatPage];
            };
            vc.smsLoginBlock = ^{
                [weakSelf switchToSmsPage];
            };
            vc.loginResponseBlock = ^(RBLoginResponse * _Nonnull response) {
                [weakSelf loginEvent:response removeVcCount:0];
            };
            [self addChildViewController:vc];
            vc;
        });
    }
    return _passwordVC;
}

- (RBSmsLoginVC *)smsVC
{
    if (!_smsVC)
    {
        self.smsVC = ({
            RBSmsLoginVC *vc = RBSmsLoginVC.new;
            __weak typeof(self) weakSelf = self;
            vc.wechatLoginBlock = ^{
                [weakSelf switchToWechatPage];
            };
            vc.passwordLoginBlock = ^{
                [weakSelf switchToPasswordPage];
            };
            vc.loginResponseBlock = ^(RBLoginResponse * _Nonnull response) {
                [weakSelf loginEvent:response removeVcCount:0];
            };
            [self addChildViewController:vc];
            vc;
        });
    }
    return _smsVC;
}

@end
